package com.lam.datasave.diary;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import com.lam.fundamental.R;

public class DiaryActivity extends ListActivity{
	
	
	public static final int CREATE_DIARY=1;
	public static final int EDIT_DIARY=2;
	
	
	public static final int addDiary=Menu.FIRST;
	public static final int deleteDiary=Menu.FIRST+1;
	
	private DiaryDBAdapter dbAdapter;
	private Cursor diaryCurcor;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, addDiary, 0, "Add Diary");
		menu.add(0, deleteDiary, 0, "Delete Diary");
		return super.onCreateOptionsMenu(menu);
	}

	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Cursor cursor=diaryCurcor;
		cursor.moveToPosition(position);
		Intent intent=new Intent(this,DiaryEditActivity.class);
		intent.putExtra(DiaryDBAdapter.KEY_ROWID, id);
		intent.putExtra(DiaryDBAdapter.KEY_TITLE, cursor.getString(
				cursor.getColumnIndexOrThrow(DiaryDBAdapter.KEY_TITLE)));
		intent.putExtra(DiaryDBAdapter.KEY_CONTENT, cursor.getString(
				cursor.getColumnIndexOrThrow(DiaryDBAdapter.KEY_CONTENT)));
		startActivityForResult(intent, EDIT_DIARY);
	}


	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case addDiary:
			createDiary();
			return true;
		case deleteDiary:
			if(!getListView().isSelected()){
				Toast.makeText(this, "Please select an item!", Toast.LENGTH_SHORT).show();
			}else
			dbAdapter.getDbHelper().deleteDiaryById(getListView().getSelectedItemId());
			return true;
		default:
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		dbAdapter=new DiaryDBAdapter(this);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ds_diary_list);
		renderListView();
	}
	
	public void createDiary(){
		Intent intent = new Intent(this,DiaryEditActivity.class);
		startActivityForResult(intent,CREATE_DIARY);
	}
	
	private void renderListView(){
		diaryCurcor=dbAdapter.getDbHelper().getAllNotes();
		startManagingCursor(diaryCurcor);
		String[] from = new String[]{
			DiaryDBAdapter.KEY_TITLE,
			DiaryDBAdapter.KEY_CREATED
		};
		int[] to =new int[] {R.id.tv_diaryTitle,R.id.tv_diaryCreatedTime};
		if(diaryCurcor.getCount()!=0){
			SimpleCursorAdapter notesAdapter=new SimpleCursorAdapter(this, 
					R.layout.ds_diarylsit, diaryCurcor, from, to,0);
			setListAdapter(notesAdapter);
		}
		
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		renderListView();
	}
	
	
	
}
